package com.live.receiver;

import java.io.File;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.live.tools.LiveTools;

public class BootReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		File fileFolder = context.getFilesDir();
		File[] files = fileFolder.listFiles();
		for (int i = 0; i < files.length; i++) {

			// 指定类型文件
			if (files[i].getName().indexOf("alarm") == 0) {

				// 取得现有提醒表
				List<Map<String, Object>> liveAlarmList = LiveTools.readLiveAlarmFile(context, files[i].getName());

				for (int j = 0; j < liveAlarmList.size(); j++) {

					Map<String, Object> liveAlarm = liveAlarmList.get(j);

					// 创建AlarmManager
					@SuppressWarnings("static-access")
					AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);

					// 为系统添加定时提醒
					PendingIntent pi = PendingIntent.getBroadcast(context, Integer.valueOf(liveAlarm.get("matchId")
							.toString()), new Intent(context, LiveReceiver.class), 0);

					// 设定提醒时间
					String alarmTims[] = liveAlarm.get("matchTime").toString().split(",");
					Calendar c = Calendar.getInstance();
					c.set(Integer.valueOf(alarmTims[0]), Integer.valueOf(alarmTims[1]), Integer.valueOf(alarmTims[2]),
							Integer.valueOf(alarmTims[3]), Integer.valueOf(alarmTims[4]), 0);
					am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
				}
			}
		}
	}
}
